package com.sihan.swvp.action.worksy;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.entity.worksy.Taskxs;
import com.sihan.swvp.service.sys.LogService;
import com.sihan.swvp.service.worksy.TaskImpService;
import com.sihan.swvp.service.worksy.TaskxsService;

@Controller
@RequestMapping("/taskxs")
public class TaskxsAction extends BaseAction{

	@Autowired
	private TaskxsService taskxsService;
	
	@Autowired
	private LogService logService;
	@Autowired
	private TaskImpService impService;
	

	@ResponseBody
	@RequestMapping("/list")
	public Object getList(Integer page,Integer limit){
		Map<String, Object> kv = new HashMap<String, Object>();
		kv.put("bdz", getParameter("bdz"));
		kv.put("bz", getParameter("bz"));
		kv.put("zy", getParameter("zy"));
		kv.put("dydj", getParameter("dydj"));
		kv.put("startTime",getParameter("startTime"));//巡视开始时间
		kv.put("endTime",getParameter("endTime"));//巡视结束时间
		kv.put("isfinish",getParameter("isfinish"));//巡视是否完成 （1-完成，0-未完成）
		kv.put("file",getParameter("file"));//是否归档（1-归档，0-未归档）
		kv.put("type",getParameter("type"));//巡视类型
		kv.put("content",getParameter("content"));//巡视类型
		
		return pageData( taskxsService.getList(page, limit,kv));
	}

	@ResponseBody
	@RequestMapping("/edit")
	public Object editWorksy(Taskxs taskxs){
		try {
			taskxs.setUpuser(getLoginUser().getId());
			
			taskxs.setStarttime(CommonUtil.o2date(getParameter("starttime")));
			taskxs.setEndtime(CommonUtil.o2date(getParameter("endtime")));
			taskxsService.addTaskxs(taskxs);
			if(taskxs.getId() != null && !"".equals(taskxs.getId())){
				logService.addUserLog("修改巡视任务", "巡视任务", getLoginUser().getId(), "修改巡视任务："+taskxs.getContent(),getRemoteAddr(),1,0);
			}else{
				logService.addUserLog("添加巡视任务", "巡视任务", getLoginUser().getId(), "添加巡视任务："+taskxs.getContent(),getRemoteAddr(),1,0);
			}
			return successMsg("保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			if(taskxs.getId() != null && !"".equals(taskxs.getId())){
				logService.addUserLog("修改巡视任务", "巡视任务", getLoginUser().getId(), "修改巡视任务："+taskxs.getContent(),getRemoteAddr(),0,0);
			}else{
				logService.addUserLog("添加巡视任务", "巡视任务", getLoginUser().getId(), "添加巡视任务："+taskxs.getContent(),getRemoteAddr(),0,0);
			}
			return failMsg("保存失败！");
		} 
	}

	@ResponseBody
	@RequestMapping("/del")
	public Object delById(String id,String content){
		try {
			taskxsService.delById(id);
			logService.addUserLog("删除巡视任务", "巡视任务", getLoginUser().getId(), "删除巡视任务："+content,getRemoteAddr(),1,0);
			return successMsg("删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除巡视任务", "巡视任务", getLoginUser().getId(), "删除巡视任务："+content,getRemoteAddr(),0,0);
			return failMsg("删除失败！");
		}
	}
	
	@RequestMapping("/impExcel")
	protected void impExcel(@RequestParam("file")MultipartFile file,HttpServletResponse response){
		ServletOutputStream output = null;
		Object msg = null;
		try {
			response.setContentType("text/html;charset=UTF-8");
			output = response.getOutputStream();
			String name = file.getOriginalFilename();
			String fileType = name.substring(name.lastIndexOf('.'));
			if(!(".xls".equals(fileType) || ".xlsx".equals(fileType))){
				msg = failMsg("文件格式错误，请上传Excel文件。");
				return ;
			}
			try {
				impService.addxsImp(file, getLoginUser().getId());
				msg = successMsg("导入数据成功。");
				return ;
			} catch (Exception e) {
				if(e instanceof Exception){
					logService.addUserLog("任务导入","巡视任务导入", 
							Integer.valueOf(getLoginUser().getId().toString()), e.getMessage(),getRemoteAddr(),0,0);
					msg = failMsg("错误："+e.getMessage());
					return ;
				}
				else{
					msg = failMsg("错误：系统未知错误。");
					return ;
				}
			}
		} catch (Exception e) {
			
			msg = failMsg("错误，请检查文件数据格式是否正确。");
			return ;
		}
		finally{
			try {
				output.write(JSON.toJSONString(msg).getBytes("utf-8"));
				output.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		    
			CommonUtil.close(output);
		}
	}
}
